package com.google.code.rgbgoesbetween.test;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

import com.google.code.rgbgoesbetween.data.AccountStatus;
import com.google.code.rgbgoesbetween.gui.HomeActivity;
import com.google.code.rgbgoesbetween.gui.LoginActivity;
import com.google.code.rgbgoesbetween.services.DataProvider;
import com.google.code.rgbgoesbetween.services.SessionManager;
import com.jayway.android.robotium.solo.Solo;

/**
 * Tests to see how LoginActivity works under a variety of inputs using dummy
 * database backend and dummy hasing function.
 * 
 * @author pviolette
 * 
 */
public class LoginActivityTestInProduction extends
		ActivityInstrumentationTestCase2<LoginActivity> {
	private static final int USERNAME_TEXT = 0;
	private static final int PASSWORD_TEXT = 1;
	private static final int SUBMIT_BUTTON = 1;
	/**
	 * Robotium tester...allows us to mock user inputs. This is built off of
	 * Android testing framework, but is much easier to use.
	 * 
	 */
	private Solo solo;

	public LoginActivityTestInProduction() {
		super(LoginActivity.class);
	}

	/**
	 * This runs BEFORE onCreate is called... For an integration test, we will
	 * let the app run without meddling too much with the implementation.
	 */
	@Override
	public void setUp() {
		solo = new Solo(getInstrumentation(), getActivity());
	}

	public void testUserLogsInProduction() {
		SessionManager.getSessionManager().init();
		DataProvider.getDataProvider().init(getActivity());
		AccountStatus as = SessionManager.getSessionManager().createProfile("bob1", "123");
		Log.d("PRODUCTION_TEST", as.name());
		int USERNAME_TEXT = 0;
		int PASSWORD_TEXT = 1;
		int SUBMIT_BUTTON = 1;
		solo.clearEditText(USERNAME_TEXT);
		solo.enterText(USERNAME_TEXT, "bob1");
		solo.clearEditText(PASSWORD_TEXT);
		solo.enterText(PASSWORD_TEXT, "123");
		solo.clickOnButton(SUBMIT_BUTTON);
		solo.assertCurrentActivity(
				"We should be logged in with a valid user/password",
				HomeActivity.class);
	}

	public void testUserDoesNotExist() {
		solo.clearEditText(USERNAME_TEXT);
		solo.enterText(USERNAME_TEXT, "Trolololol");
		solo.clearEditText(PASSWORD_TEXT);
		solo.enterText(PASSWORD_TEXT, "123");
		solo.clickOnButton(SUBMIT_BUTTON);
		solo.assertCurrentActivity("Invalid user should stay in LoginActivity",
				LoginActivity.class);
		// TODO Verify notification text gets set
	}

	public void testUserExistsWrongPassword() {
		DataProvider.getDataProvider().init(this.getActivity());
		SessionManager.getSessionManager().init();
		SessionManager.getSessionManager().createProfile("Bob","123");
		solo.clearEditText(USERNAME_TEXT);
		solo.enterText(USERNAME_TEXT, "Bob");
		solo.clearEditText(PASSWORD_TEXT);
		solo.enterText(PASSWORD_TEXT, "Wrong password");
		solo.clickOnButton(SUBMIT_BUTTON);
		solo.assertCurrentActivity("Invalid user should stay in LoginActivity",
				LoginActivity.class);
		// TODO Verify notification text gets set
	}
	
	public void testUserLockout() {
		SessionManager.getSessionManager().init();
		DataProvider.getDataProvider().init(getActivity());
		AccountStatus as = SessionManager.getSessionManager().createProfile("boblockout", "123");
		Log.d("PRODUCTION_TEST", as.name());
		int USERNAME_TEXT = 0;
		int PASSWORD_TEXT = 1;
		int SUBMIT_BUTTON = 1;
		for (int i = 0; i < 3; i++) { // Enter invalid data 3 times
			solo.clearEditText(USERNAME_TEXT);
			solo.enterText(USERNAME_TEXT, "boblockout");
			solo.clearEditText(PASSWORD_TEXT);
			solo.enterText(PASSWORD_TEXT, "321");
			solo.clickOnButton(SUBMIT_BUTTON);
		}
		
		// Even if valid, should be locked out.
		solo.clearEditText(USERNAME_TEXT);
		solo.enterText(USERNAME_TEXT, "boblockout");
		solo.clearEditText(PASSWORD_TEXT);
		solo.enterText(PASSWORD_TEXT, "123");
		solo.clickOnButton(SUBMIT_BUTTON);
		solo.assertCurrentActivity(
				"We should be locked out of a user.",
				LoginActivity.class);
	}

	@Override
	public void tearDown() {
		solo.finishOpenedActivities();
	}

}
